<?php
/**
 * Created by PhpStorm.
 * User: zdw
 * Date: 2017/6/27
 * Time: 22:20
 */
namespace app\admin\controller;
use app\common\Tree;
class Menu extends  Base{

    public function index(){
        $menu=db('menu')->select();
        $config=[
            'sid'=>'id','pid'=>'pid','name'=>'name'
        ];
        $tree = new Tree($config);
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $array = array();
        foreach($menu as $r) {
            $r['cname'] = $r['name'];
            if($r['pid']==0){
                $r['str_manage'] = '<a href="'.U('menu/add',array('id'=>$r['id'],'id'=>$r['id'])).'" class="btn btn-secondary btn-sm btn-icon icon-left">
										添加下级</a> |

									<a href="'.U('menu/edit',array('id'=>$r['id'])).'" class="btn btn-danger btn-sm btn-icon icon-left">
										 编辑									</a> |

									<a id="ax_'.$r['id'].'" href="javascript:void(0)" onclick="del('.$r['id'].')" class="btn btn-info btn-sm btn-icon icon-left">
										 删除								</a>						';
            }else{
                $r['str_manage'] = '
									<a href="'.U('menu/edit',array('id'=>$r['id'])).'" class="btn btn-danger btn-sm btn-icon icon-left">
										 编辑							</a> |

									<a id="ax_'.$r['id'].'" href="javascript:void(0)" onclick="del('.$r['id'].')" class="btn btn-info btn-sm btn-icon icon-left">
										 删除							</a>						';
            }
            if($r['display']==0){
                $r['display_str'] ='<a href="javascript:void(0)" onclick="ajax_display('.$r['id'].')" id="dis_'.$r['id'].'"><font color="red"> 不显示</color></a>';
            }else{
                $r['display_str'] ='<a href="javascript:void(0)" onclick="ajax_display('.$r['id'].')" id="dis_'.$r['id'].'"> 显示</a>';
            }
            $array[] = $r;
        }
        $str  = "<tr  >
				<td align='center'>\$id</td>
				<td>\$spacer\$name</td>
				<td align='center'>\$ordid</td>
				 <td align='center'>\$display_str</td>
				<td align='center'>\$str_manage</td>
				</tr>";
        $tree->init($array);

        $data  = $tree->get_tree(0, $str);
     $this->assign("data",$data);
      return   $this->fetch();
    }

    public function ajaxShow(){
        $id=I("get.id",0,'intval');
        $menu=db('menu')->where(['id'=>$id])->find();
        $menu || die(json_error('参数错误'));
        $update_display=$menu['display']>0?0:1;
        $data=[
            'display'=>$update_display
        ];
        $res=db('menu')->where(['id'=>$id])->update($data);
        $name=['不显示','显示'];
        if($res) die(json_success('ok',['name'=>$name[$update_display]]));
        die(json_error('修改失败'));
    }

    public function ajaxDelete(){
        $id=I("get.id",0,'intval');
        $model=db('menu');
        $menu=$model->where(['id'=>$id])->find();
        $menu || die(json_error('参数错误'));
        $list=$model->where(['pid'=>$id])->select();
        $list && die(json_error('有下级无法删除'));
        $res=$model->where(['id'=>$id])->delete();
        $res || die(json_error('删除失败'));
        die(json_success('成功'));
    }

    public function add(){
        $model=db('menu');
        if($_POST){
            $pid=I("post.pid",0);
            $name=I("post.name",'','trim');
            $controller=I("post.module_name",'','trim');
            $action=I("post.action_name",'','trim');
            $para=I("post.other_para",'');
            $sort=I("post.ordid",255,'intval');

            if($pid>0){
                $num=db('menu')->where(['id'=>$pid])->count();
                $num>0 || die(json_error('参数错误'));
            }
            $name || die(json_error('菜单名称不能为空'));
            $controller || die(json_error('控制器名称不能为空'));
            $action || die(json_error('方法名不能为空'));
            $data=[
                'pid'=>$pid,
                'name'=>$name,
                'module_name'=>$controller,
                'action_name'=>$action,
                'other_para'=>$para,
                'ordid'=>$sort
            ];

            $res=$model->insert($data);
            $res || die(json_error('添加失败'));
            die(json_success('添加成功',['url'=>U('menu/index')]));
        }

        $id=I("param.id",0);
        $menu_list=$model->where(['pid'=>0])->select();
        $this->assign('id',$id);
        $this->assign('list',$menu_list);
       return $this->fetch();
    }

    public function edit(){
        $model=db('menu');
        if($_POST){
            $pid=I("post.pid",0);
            $id=I("post.id",0,'intval');
            $name=I("post.name",'','trim');
            $controller=I("post.module_name",'','trim');
            $action=I("post.action_name",'','trim');
            $para=I("post.other_para",'');
            $sort=I("post.ordid",255,'intval');

            if($pid>0){
                $num=db('menu')->where(['id'=>$pid])->count();
                $num>0 && die(json_error('参数错误'));
            }
            $name || die(json_error('菜单名称不能为空'));
            $controller || die(json_error('控制器名称不能为空'));
            $action || die(json_error('方法名不能为空'));
            $data=[
                'pid'=>$pid,
                'name'=>$name,
                'module_name'=>$controller,
                'action_name'=>$action,
                'other_para'=>$para,
                'ordid'=>$sort
            ];
            $res=$model->where(['id'=>$id])->update($data);
            $res || die(json_error('添加失败'));
            die(json_success('添加成功'));
        }

        $id=I("param.id",0,'intval');

        $menu_list=$model->where(['pid'=>0])->select();
        $menu=$model->where(['id'=>$id])->find();

        $menu || die(json_error('参数错误'));
        $this->assign('id',$menu['pid']);
        $this->assign('list',$menu_list);
        $this->assign('info',$menu);
        return $this->fetch();
    }

}